# -*- coding: utf-8 -*-


# cmap2owl -- Helper application to convert from concept maps to OWL ontologies
# Copyright (c) 2008-2013  Rodrigo Rizzi Starr
#  
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#  
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#  
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.


'''
This contains a class to show a progress bar with some additional messages

@author: Rodrigo Rizzi Starr
@copyright: Copyright © 2008-2013 Rodrigo Rizzi Starr
@license: MIT License
@contact: rodrigo.starr@gmail.com
'''


from __future__ import division
import sys

# GTK+ imports
import pygtk
if not sys.platform == 'win32':
    pygtk.require('2.0')
try:
    import gtk
except:
    sys.exit(1)

# Project imports
from log import log
log.debug("Loaded...")

class ProgressBox(object):
    "Draws a configurable progressBox"
    
    def __init__(self, mainWindow):
        self.mainWindow = mainWindow
        
        self.sizeHint = 0
        self.currentPosition = 0
        self.fraction = 0.0
        
        # Interface objects
        self.box = None
        self.progressBar = None
        self.panel = None
        self.topLabel = None
        self.bottomLabel = None
    
    def setSizeHint(self, hint):
        "Sets the predicted size"
        self.sizeHint = hint
    
    def build(self):
        "Builds the progress box"
        
        self.topLabel = gtk.Label('Default')
        
        
        self.bottomLabel = gtk.Label('Default')
        
        
        self.progressBar = gtk.ProgressBar()
        self.progressBar.set_size_request(250, 20)
        
        
        self.panel = gtk.VBox(homogeneous=False, spacing=3)
        self.panel.set_homogeneous(True)
        self.panel.set_size_request(300, 60)
        self.panel.pack_start(self.topLabel)
        self.panel.pack_start(self.progressBar)
        self.panel.pack_start(self.bottomLabel)
        
        self.box = gtk.Window()
        self.box.set_title('Thinking...')
        self.box.set_position(gtk.WIN_POS_CENTER)
        self.box.set_transient_for(self.mainWindow)
        self.box.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_DIALOG)
        self.box.set_resizable(False)
        self.box.set_modal(True)
        self.box.set_border_width(5)
        self.box.add(self.panel)

    def show(self):
        "Show the box"
        
        self.reset()
        
        if self.box == None:
            self.build()
        self.box.show_all()
        
    def hide(self):
        "Hides (in fact, destroys) the box"
        self.box.hide_all()
        self.reset()
    
    def reset(self):
        "Puts the fraction back to zero again..."
        self.fraction = 0.0
        self.currentPosition = 0
    
    def pulse(self):
        "Shows some progress"
        
        if self.sizeHint == 0:
            self.progressBar.pulse()
        else:
            self.fraction += 1.0/self.sizeHint
            self.currentPosition += 1
            self.progressBar.set_fraction(min(1.0, self.fraction))
            self.progressBar.set_text(str(self.currentPosition))
        while gtk.events_pending():
            gtk.main_iteration(False)

    
    def setMainLabel(self, label):
        "Defines the main label text"
        self.topLabel.set_label(label)
        
    def getMainLabel(self):
        "Retrieves the main label text"
        return self.topLabel.get_label()
    mainLabel = property(getMainLabel, setMainLabel)
        
    def setSecundaryLabel(self, label):
        "Defines the main label text"
        self.bottomLabel.set_label(label)
        
    def getSecundaryLabel(self):
        "Retrieves the main label text"
        return self.bottomLabel.get_label()
    secundaryLabel = property(getSecundaryLabel, setSecundaryLabel)
        
        
        
